<template>
    <div class="content">
        <div class="clearfix">
            <Form class="f-fl" inline :label-width="5">
                <FormItem>
                    <Input
                        v-model="params.name"
                        placeholder="用户名/姓名"
                    ></Input>
                </FormItem>
                <FormItem>
                    <RemoteSelect
                        v-model="params.schoolId"
                        :label="schoolName"
                        valueKey="schoolId"
                        labelKey="schoolName"
                        :remoteFn="querySchool"
                    ></RemoteSelect>
                </FormItem>
                <FormItem>
                    <Select
                        style="width: 140px;"
                        placeholder="状态"
                        v-model="params.status"
                        clearable
                    >
                        <Option :value="1">有效</Option>
                        <Option :value="0">失效</Option>
                    </Select>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="search">搜索</Button>
                </FormItem>
                <FormItem>
                    <Button @click="clearSearch">清空</Button>
                </FormItem>
            </Form>
            <div class="f-fr">
                <Button type="primary" @click="handleAddUser">新建用户</Button>
            </div>
        </div>
        <Table :data="list" :columns="columns" border align="center">
            <template slot-scope="{ row }" slot="userName">
                <div>{{ row.userName }}</div>
            </template>
            <template slot-scope="{ row }" slot="realName">
                <div>{{ row.realName }}</div>
            </template>
            <template slot-scope="{ row }" slot="schoolName">
                <div>{{ row.schoolName }}</div>
            </template>
            <template slot-scope="{ row }" slot="enable">
                <div>{{ row.enable == 1 ? "有效" : "失效" }}</div>
            </template>
            <template slot-scope="{ row }" slot="createTime">
                <div>{{ row.createTime }}</div>
            </template>
            <template slot-scope="{ row }" slot="action">
                <div class="table-action">
                    <Button
                        type="text"
                        @click="handleEditUser(row.userId)"
                    >编辑</Button>
                    <Button
                        type="text"
                        @click="handleDelete(row.userId)"
                    >删除</Button>
                    <Button
                        type="text"
                        @click="handleRestPwd(row.userId)"
                    >重置密码</Button>
                </div>
            </template>
        </Table>
        <Pagination
            :pageSize.sync="params.pageSize"
            :current.sync="params.pageNum"
            :total="params.total"
            @change="queryList"
        />
    </div>
</template>
<script>
import { schoolManageList } from '@/api/schoolManage'
import { queryUserManageList, deleteUserManage } from '@/api/userManage'
import { resetPwd } from '@/api/user'
import formatTime from '@/libs/formatTime'
import RemoteSelect from '@/components/remote-select'

export default {
    inject: ['nextPageReload'],
    components: {
        RemoteSelect
    },
    data () {
        const { schoolId = '', schoolName = '' } = this.$route.query
        return {
            schoolName,
            params: {
                total: 0,
                status: '',
                pageNum: 1,
                pageSize: 10,
                name: '',
                schoolId
            },
            list: [],
            columns: Object.freeze([
                { title: '序号', type: 'index', align: 'center', width: 80 },
                { title: '用户名', slot: 'userName', align: 'center' },
                { title: '姓名', slot: 'realName', align: 'center' },
                { title: '学校名称', slot: 'schoolName', align: 'center' },
                { title: '创建时间', slot: 'createTime', align: 'center' },
                { title: '状态', slot: 'enable', align: 'center' },
                { title: '操作', slot: 'action', align: 'center', width: 140 }
            ])
        }
    },
    mounted () {
        this.queryList()
    },
    methods: {
        search () {
            this.params.pageNum = 1
            this.queryList()
        },
        querySchool (key) {
            return schoolManageList({
                pageNum: 1,
                pageSize: 20,
                schoolName: key
            })
        },
        queryList () {
            const enable =
                typeof this.params.status !== 'number'
                    ? ''
                    : this.params.status === 1
            queryUserManageList({
                ...this.params,
                enable
            }).then(res => {
                if (res.data.code === '0000') {
                    const data = res.data.data
                    data.list.forEach(item => {
                        item.createTime = formatTime.fullTime(item.createTime)
                    })
                    this.params.total = data.total
                    this.list = data.list
                } else {
                    this.$Message.warning(res.data.msg)
                }
            })
        },
        clearSearch () {
            this.params = {
                total: 0,
                status: '',
                pageNum: 1,
                pageSize: 10,
                name: '',
                schoolId: ''
            }
            this.queryList()
            this.$router.replace({
                name: this.$route.name
            })
        },
        handleDelete (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '确认是否删除？',
                onOk: () => {
                    deleteUserManage(id).then(res => {
                        if (res.data.code === '0000') {
                            this.$Message.success('删除成功')
                            this.params.pageNum = 1
                            this.queryList()
                        } else {
                            this.$Message.error(res.data.msg)
                        }
                    })
                }
            })
        },
        handleAddUser () {
            this.nextPageReload()
            this.$router.push({
                path: '/userManage/add'
            })
        },
        handleEditUser (id) {
            this.nextPageReload()
            this.$router.push({
                name: 'userManageEdit',
                params: {
                    id
                }
            })
        },
        handleRestPwd (id) {
            this.$Modal.confirm({
                title: '提示',
                content: '确认是否重置密码？',
                onOk: () => {
                    resetPwd(id).then(res => {
                        if (res.data.code === '0000') {
                            this.$Message.success({
                                content: `重置成功，新密码：${res.data.msg}`,
                                duration: 4
                            })
                        } else {
                            this.$Message.error(res.data.msg)
                        }
                    })
                }
            })
        }
    }
}
</script>
